<?php
//
// use App\Models\Order;
// use App\Models\OtherWithholdLog;
// use App\Services\AlipayService;
// use App\Services\AntchainService;
// use App\Services\CommonService;
// use App\Services\EsignService;
// use App\Services\OrderService;
//
// //小程序未支付订单取消
// //小程序未支付订单取消
// protected function noPayOrderCancel($order_id, $user_id, $member_id)
// {
//     if ($user_id) {
//         $where[] = ['uid', $user_id];
//     }
//     if ($member_id) {
//         $where[] = ['seller_id', $member_id];
//     }
//     $where[] = ['id', $order_id];
//     //221017
//     $order = $this->where($where)->wherein('status', [OrderService::ORDER_STATUS_NOT_PAY, OrderService::ORDER_STATUS_SIGN_NOTPAY, OrderService::ORDER_STATUS_SIGN_NOCHECK, OrderService::ORDER_STATUS_PAY_SUCCESS])
//         ->select('id', 'mini_id', 'uid', 'trade_no', 'rent_total', 'status', 'pay_type', 'rights_auth_no', 'alipay_trade_no', 'sign_status', 'flow_id', 'create_time')->first();
//     $order = $order ? $order->toArray() : [];
//     if (empty($order)) {
//         return ['status' => CommonService::HTTP_CODE_FAIL, 'msg' => '订单不存在', 'data' => []];
//     }
//     if ($order['status']>OrderService::ORDER_STATUS_PAY_SUCCESS) {
//         return ['status' => CommonService::HTTP_CODE_FAIL, 'msg' => '当前订单状态不支持取消', 'data' => []];
//     }
//
//     if($order['flow_id']&&$order['status']==OrderService::ORDER_STATUS_SIGN_NOTPAY){
//          AntchainService::paytradeCancel($order['flow_id']);
//     }
//
//     if (!empty($order['rights_auth_no']&&$order['alipay_trade_no'])&&$order['status']<=OrderService::ORDER_STATUS_PAY_SUCCESS) {//解冻预授权，作废合同
//         $alipay_data['alipayTradeNo'] = $order['alipay_trade_no'];
//         $alipay_data['authNo'] = $order['rights_auth_no'];
//         $prequery = AlipayService::alipayPreQuery($order['mini_id'], $alipay_data);
//         // dd($prequery);
//         if ($prequery['status']) {
//             $rest_amount = $prequery['data']['rest_amount'];//订单总共剩余的冻结金额
//             if ($rest_amount > 0) { //冻结金额大于0  执行解冻操作
//                 $thaw_data['amount'] = $rest_amount;
//                 $thaw_data['authNo'] = $order['rights_auth_no'];
//                 $thaw_query = AlipayService::alipayPreThaw($thaw_data);
//                 if ($thaw_query['status'] && $thaw_query['data']['code'] == AlipayService::ALIPAY_SUCCESS_CODE) { //解冻成功
//                     $res = $this->where('id', $order_id)->update(['status' => OrderService::ORDER_STATUS_CENCEL]);
//                     if ($res) {
//                         if (!empty($order['flow_id']) && $order['sign_status'] == EsignService::ESIGN_STATUS_SIGNING) {//合同未签署完成撤销
//                             EsignService::revoke($order['flow_id']);
//                         }
//                         //查找扣款失败未履约的订单
//                         $log = OtherWithholdLog::where(['order_no' => $order['trade_no'], 'status' => 0])->whereIn('pay_type', [OtherWithholdLog::PAY_TYPE_ZM, OtherWithholdLog::PAY_TYPE_MANUAL_ZM])
//                             ->groupBy('out_trade_no')
//                             ->get();
//                         foreach ($log as $value) {
//                             $orderData = [
//                                 'orderId' => $value['order_no'],
//                                 'outTradeNo' => $value['out_trade_no'],
//                                 'tradeNo' => $value['trade_no'],
//                                 'status' => 'CLOSED',
//                             ];
//                             AlipayService::orderInfo($orderData);
//                         }
//                         $user_order_count = Order::where('uid', $order['uid'])->whereIn('status', OrderService::getOrderStatus(OrderService::ORDER_MANUAL_WITHHOLD_OR_PAY))->sum('amount');
//                         Order::where('uid', $order['uid'])->whereIn('status', OrderService::getOrderStatus(OrderService::ORDER_MANUAL_WITHHOLD_OR_PAY))->update(['user_order_count' => $user_order_count]);
//                     } else {
//                         return ['status' => CommonService::HTTP_CODE_FAIL, 'msg' => '取消失败', 'data' => []];
//                     }
//                 } else {
//                     return ['status' => CommonService::HTTP_CODE_FAIL, 'msg' => '授权资金解冻失败，请联系平台客服解决', 'data' => []];
//                 }
//             } else {
//                 $res = $this->where('id', $order_id)->update(['status' => OrderService::ORDER_STATUS_CENCEL]);
//                 if ($res) {
//                     if (!empty($order['flow_id']) && $order['sign_status'] == EsignService::ESIGN_STATUS_SIGNING) {//合同未签署完成撤销
//                         EsignService::revoke($order['flow_id']);
//                     }
//                 } else {
//                     return ['status' => CommonService::HTTP_CODE_FAIL, 'msg' => '取消失败', 'data' => []];
//                 }
//             }
//         } else {
//             return ['status' => CommonService::HTTP_CODE_FAIL, 'msg' => '未查到授权资金解冻，请联系平台客服解决', 'data' => []];
//         }
//     }
//
//     $res = $this->where('id', $order_id)->update(['status' => OrderService::ORDER_STATUS_CENCEL]);
//     if (!$res) {
//         return ['status' => CommonService::HTTP_CODE_FAIL, 'msg' => '取消失败', 'data' => []];
//     }
//
//    return ['status' => CommonService::HTTP_CODE_SUCCESS, 'msg' => '取消成功', 'data' => []];
//
//
//
//
//
// }
